//
//  GGRegisterViewController.m
//  DDLock
//
//  Created by ios on 2017/12/25.
//  Copyright © 2017年 saiyi. All rights reserved.
//

#import "GGRegisterViewController.h"

@interface GGRegisterViewController ()<UITextFieldDelegate>
{
    NSTimer *_timer;
    NSInteger count;
}
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *veriCodeTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UITextField *confirmTextField;
@property (weak, nonatomic) IBOutlet UIButton *verBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *verBtnW;

@end

@implementation GGRegisterViewController

- (void)setDidLoad {
    
    
    self.backView.layer.cornerRadius = 10.0f;
    self.backView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.backView.layer.shadowOpacity = 0.2;
    self.backView.layer.shadowOffset = CGSizeMake(3, 3);
    self.backView.layer.shadowRadius = 10;
    
    self.phoneTextField.delegate = self;
    self.veriCodeTextField.delegate = self;
    self.pwdTextField.delegate = self;
    self.confirmTextField.delegate = self;
    
    self.confirmBtn.layer.cornerRadius = 5.0f;
    self.confirmBtn.layer.masksToBounds = YES;
    self.verBtn.layer.cornerRadius = 10.0f;
    self.verBtn.layer.masksToBounds = YES;
    self.verBtnW.constant = 100;
    count = 60;
    
}
// 获取验证码
- (IBAction)clickVerBtn:(id)sender {
    
    
    NSString *phoneNum = self.phoneTextField.text;
    if ([NSString isMobileNumber:phoneNum]) {
        [UIView showTipsWithText:@"手机号格式错误"];
        return;
    }
    
    // 发送网络请求
    
    // 成功后开始倒计时
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(repeatCountReduce) userInfo:nil repeats:YES];
}
// 确认
- (IBAction)clickConfirmBtn:(UIButton *)sender {
    
    NSString *veriCodeStr = self.veriCodeTextField.text;
    NSString *pwdStr = self.pwdTextField.text;
    NSString *conPwdStr = self.confirmTextField.text;
    
    if ([NSString isBlankString:veriCodeStr]) {
        [UIView showTipsWithText:@"验证码不能为空"];
        return;
    }
    
    if (![pwdStr isEqualToString:conPwdStr]) {
        [UIView showTipsWithText:@"密码不统一请重新输入密码"];
        return;
    }
    
    if (pwdStr.length < 6) {
        [UIView showTipsWithText:@"请输入6位以上字母数字密码"];
        return;
    }
    
    
    
    
    
}

- (void)repeatCountReduce {
    self.verBtnW.constant = 50;
    count--;
    self.verBtn.enabled = NO;
    [self.verBtn setTitle:[NSString stringWithFormat:@"%zds", count] forState:UIControlStateNormal];
    if (count <= 0) {
        [_timer invalidate];
        _timer = nil;
        self.verBtn.enabled = YES;
        self.verBtnW.constant = 100;
        [self.verBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
}

- (void)dealloc {
    [_timer invalidate];
    _timer = nil;
}


#pragma mark - UITextFieldDelegate



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSInteger tag = textField.tag;
    NSString *str = textField.text;
    if (0 == tag) {  // 手机号
        if (![NSString isNumber:string]) {
            [UIView show:kAnimationType_FADE text:@"手机号必须是数字"];
            return NO;
        }
        if (![NSString isNumber:string]) {
            [UIView show:kAnimationType_FADE text:@"手机号必须是数字"];
            return NO;
        }
        if (![string isEqualToString:@""]) {
            str = [str stringByAppendingString:string];
        } else {
            str = [str substringToIndex:str.length - 1];
        }
        if (str.length > 11) {
            return NO;
        }
    } else if(2 == tag || 3 == tag) { // 密码
        if (![NSString isNumberOrLetter:string]) {
            [UIView showTipsWithText:@"密码只能为数字或字母"];
            return NO;
        }
    }
    
    return YES;
    
}


@end
